#include <QString>
#include <QMessageBox>
#include <QThread>
#include <QCoreApplication>
#include "MainWindow.h"

CBotWorker::CBotWorker(CMainWindow *MainWindow) :
m_MainWindow(MainWindow)
{
    m_Bot = new CBotCombats;
}

CBotWorker::~CBotWorker()
{
    delete m_Bot;
}

void CBotWorker::Process()
{
    if (m_MainWindow->GetNickEdit()->text().isEmpty() || m_MainWindow->GetLoginLine()->text().isEmpty() || m_MainWindow->GetPswLine()->text().isEmpty())
    {
        CWorkerEvent *Event = new CWorkerEvent(EventID::EMPTY_FIELDS);
        QCoreApplication::postEvent(m_MainWindow, Event);
        return;
    }

    m_Bot->Reload(std::string(m_MainWindow->GetLoginLine()->text().toLocal8Bit().data()), std::string(m_MainWindow->GetPswLine()->text().toLocal8Bit().data()));
    m_Bot->SetLookCharName(std::string(m_MainWindow->GetNickEdit()->text().toLocal8Bit().data()));

    QByteArray Img;
    if (m_Bot->GetCharImage(Img))
    {
        QPixmap Pix;
        Pix.loadFromData(Img);
        CWorkerEvent *Event = new CWorkerEvent(EventID::PIXMAP);
        Event->SetPixmap(Pix);
        QCoreApplication::postEvent(m_MainWindow, Event);
    }

    STRIKES_ENTRY Strikes;
    STRIKES_ENTRY PercentStrikes;
    memset(&Strikes, 0, sizeof(STRIKES_ENTRY));
    memset(&PercentStrikes, 0, sizeof(STRIKES_ENTRY));

    if (!m_Bot->GetTotalStrikesCount(35, Strikes))
    {
        CWorkerEvent *Event = new CWorkerEvent(EventID::ERROR_STRIKES);
        QCoreApplication::postEvent(m_MainWindow, Event);
        return;
    }

    m_Bot->GetPercentageStrikeStat(Strikes, PercentStrikes);
    CWorkerEvent *Event = new CWorkerEvent(EventID::SET_VALUE);
    Event->SetStrikes(PercentStrikes);
    QCoreApplication::postEvent(m_MainWindow, Event);
}

CMainWindow::CMainWindow()
{
	m_HTabLayout           = new QHBoxLayout(this);
	m_Tab                  = new QTabWidget;
	m_MainPicture          = new QWidget(m_Tab);
	m_NickEdit             = new QLineEdit(m_Tab);
	m_AnalyzeButton        = new QPushButton(m_Tab);
	m_StrikeGroupBox       = new QGroupBox("Strikes", m_Tab);
	m_CredentialsGroupBox  = new QGroupBox("Credentials", m_Tab);
    m_LoginLine            = new QLineEdit(m_Tab);
    m_PswLine              = new QLineEdit(m_Tab);

	m_HeadSProgress  = new QProgressBar(m_Tab);
	m_ChestSProgress = new QProgressBar(m_Tab);
	m_GroinSProgress = new QProgressBar(m_Tab);
	m_LegsSProgress  = new QProgressBar(m_Tab);

    m_HeadSProgress->setMaximum(100);
    m_HeadSProgress->setMinimum(0);

    m_ChestSProgress->setMaximum(100);
    m_ChestSProgress->setMinimum(0);

    m_GroinSProgress->setMaximum(100);
    m_GroinSProgress->setMinimum(0);

    m_LegsSProgress->setMaximum(100);
    m_LegsSProgress->setMinimum(0);

    m_HeadLabel = new QLabel("Head", m_Tab);
    m_ChestLabel = new QLabel("Chest", m_Tab);
    m_GroinLabel = new QLabel("Groin", m_Tab);
    m_LegsLabel = new QLabel("Legs", m_Tab);

    m_LoginLabel = new QLabel("Login", m_Tab);
    m_PswLabel = new QLabel("Psw", m_Tab);

	m_AnalyzeButton->setText("Analyze");
    m_PswLine->setEchoMode(QLineEdit::Password);

	m_TabStrList << "Combat Analyzer";
	foreach (QString Str, m_TabStrList)
		m_Tab->addTab(new QLabel(NULL, m_Tab), Str);

	m_HTabLayout->addWidget(m_Tab);
	setLayout(m_HTabLayout);

    m_Bot = new CBotCombats;

    QPalette Pal;
    Pal.setBrush(m_MainPicture->backgroundRole(), QBrush(QPixmap(":/resuorces/Resources/unknown.jpg")));
    m_MainPicture->setPalette(Pal);
    m_MainPicture->setAutoFillBackground(true);

	m_MainPicture->setGeometry(10, 60, 76, 210);
	m_NickEdit->setGeometry(10, 30, 250, 20);
	m_AnalyzeButton->setGeometry(265, 30, 60, 20);

	m_StrikeGroupBox->setGeometry(95, 55, 230, 120);
	m_CredentialsGroupBox->setGeometry(95, 175, 230, 68);

	m_HeadSProgress->setGeometry(100, 70, 200, 20);
	m_ChestSProgress->setGeometry(100, 95, 200, 20);
	m_GroinSProgress->setGeometry(100, 120, 200, 20);
	m_LegsSProgress->setGeometry(100, 145, 200, 20);

    m_HeadLabel->setGeometry(275, 70, 30, 20);
    m_ChestLabel->setGeometry(275, 95, 30, 20);
    m_GroinLabel->setGeometry(275, 120, 30, 20);
    m_LegsLabel->setGeometry(275, 145, 30, 20);

    m_LoginLine->setGeometry(100, 190, 165, 20);
    m_PswLine->setGeometry(100, 215, 165, 20);

    m_LoginLabel->setGeometry(275, 190, 30, 20);
    m_PswLabel->setGeometry(275, 215, 30, 20);

	setFixedSize(360, 300);

    setWindowTitle("Analyzer");

	connect(m_AnalyzeButton, SIGNAL(clicked(bool)), SLOT(OnAnalyzeClick()));
}

CMainWindow::~CMainWindow()
{
    delete m_Bot;
}

QLineEdit * CMainWindow::GetNickEdit() const
{
    return m_NickEdit;
}

QLineEdit * CMainWindow::GetLoginLine() const
{
    return m_LoginLine;
}

QLineEdit * CMainWindow::GetPswLine() const
{
    return m_PswLine;
}

void CMainWindow::SetLabelVisible(bool Visible)
{
    m_HeadLabel->setVisible(Visible);
    m_ChestLabel->setVisible(Visible);
    m_GroinLabel->setVisible(Visible);
    m_LegsLabel->setVisible(Visible);
}

void CMainWindow::SetStrikesValue(STRIKES_ENTRY &Strikes)
{
    m_HeadSProgress->setValue(Strikes.Head);
    m_ChestSProgress->setValue(Strikes.Chest);
    m_GroinSProgress->setValue(Strikes.Groin);
    m_LegsSProgress->setValue(Strikes.Legs);
}

bool CMainWindow::event(QEvent* Event)
{
    if (Event->type() == QEvent::User)
    {
        CWorkerEvent* PostedEvent = (CWorkerEvent *)Event;
        QPalette Pal;
        switch (PostedEvent->GetId())
        {
            case EventID::EMPTY_FIELDS:
                QMessageBox::warning(this, tr("Analyzer"), tr("One of the necessarily fields are empty!"), QMessageBox::Ok);
                m_AnalyzeButton->setEnabled(true);
            break;

            case EventID::PIXMAP:
                Pal.setBrush(m_MainPicture->backgroundRole(), QBrush(PostedEvent->GetPixmap()));
                m_MainPicture->setPalette(Pal);
                m_MainPicture->setAutoFillBackground(true);
            break;

            case EventID::ERROR_STRIKES:
                QMessageBox::warning(this, tr("Analyzer"), tr("Can't find strikes info!"), QMessageBox::Ok);
                m_AnalyzeButton->setEnabled(true);
            break;

            case EventID::SET_VALUE:
                SetLabelVisible(false);
                SetStrikesValue(PostedEvent->GetStrikes());
                m_AnalyzeButton->setEnabled(true);
            break;
        }
        return true;
    }

    return QWidget::event(Event);
}

void CMainWindow::OnAnalyzeClick()
{
    m_AnalyzeButton->setEnabled(false);

    QThread* Thread = new QThread;
    CBotWorker *Worker = new CBotWorker(this);
 
    Worker->moveToThread(Thread);
    connect(Thread, SIGNAL(started()), Worker, SLOT(Process()));
    connect(Worker, SIGNAL(finished()), Thread, SLOT(quit()));
    connect(Worker, SIGNAL(finished()), Worker, SLOT(deleteLater()));
    connect(Thread, SIGNAL(finished()), Thread, SLOT(deleteLater()));
 
    Thread->start();
}

#include "moc_MainWindow.cpp"